﻿using B.S.DataBase.MyProject.Domain.Tickettoticket;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Implement;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.GetIcesandCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.GetIcesandCommandHander
{

    /// <summary>
    /// 开票到票采购订单
    /// 物联网-智能MES生产管理系统-07-开票到票管理V1.1-20250206
    /// </summary>
    public class GeturchaseCommandHander : IRequestHandler<GeturchaseCommand, APIResult<ApiPaging<UrchaseDto>>>
    {

        private readonly IBaseRepository<Billingder> billingderRepository;

        private readonly IBaseRepository<urchaseOrdp> urchaseOrdpRepository;

        public GeturchaseCommandHander(IBaseRepository<Billingder> billingderRepository, IBaseRepository<urchaseOrdp> urchaseOrdpRepository)
        {
            this.billingderRepository = billingderRepository;
            this.urchaseOrdpRepository = urchaseOrdpRepository;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<ApiPaging<UrchaseDto>>> Handle(GeturchaseCommand request, CancellationToken cancellationToken)
        {
            APIResult<ApiPaging<UrchaseDto>> result = new APIResult<ApiPaging<UrchaseDto>>();

            var list = from a in billingderRepository.GetAll().Result
                       join b in urchaseOrdpRepository.GetAll().Result
                        on a.BillingStae equals b.UrcStae
                       select new UrchaseDto
                       {
                           Bid = a.Bid,
                           BillingStae = a.BillingStae,
                           Billingtopic = a.Billingtopic,
                           Billingtype = a.Billingtype,
                           BillingDate = a.BillingDate,
                           Billingsale = a.Billingsale,

                           BillTicket = a.BillTicket,
                           number = a.number,
                           remark = a.remark,

                           Urcid = b.Urcid,
                           UrcStae = b.UrcStae,
                           Urcic = b.Urcic,
                           UrcName = b.UrcName,
                           UrcTime = b.UrcTime,
                           Urcamount = b.Urcamount,
                           Urceamount = b.Urceamount,
                           Urcinvoice = b.Urcinvoice,
                           Urcremark = b.Urcremark,
                       };

            // 条件过滤
            if (!string.IsNullOrEmpty(request.Stae))
            {
                list = list.Where(x => x.UrcStae.Contains(request.Stae) || x.Urcic.Contains(request.Stae));
            }

            if (!string.IsNullOrEmpty(request.UrcName))
            {
                list = list.Where(x => x.UrcName.Contains(request.UrcName));
            }

            int TotalCount = list.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.Bid).Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize);


            ApiPaging<UrchaseDto> api = new ApiPaging<UrchaseDto>();
            api.TotalCount = TotalCount;
            api.TotalPage = TotalPage;
            api.list = list.ToList();
            result.data = api;

            return Task.FromResult(result);
        }
    }
}
